<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Menu;
use app\model\Photosmodel;
use app\model\Modeltable;
use app\model\Modelfield;
use My\Tool;
use My\Validate;
use My\DataReturn;
use think\Cache;

class Publicclass extends AdminCollection
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 退出管理员
     */
    public function logout()
    {
        if ($this->getAdmin()) $this->deleteSession();
        $this->returnSuccess("安全退出成功", url('login/index'));
    }

    /**
     * 点击顶部跳转[右边内容]
     * @return string
     */
    public function maincontent()
    {
        $menuid = input('menuid');
        $url = url('index/main');
        return $url;
    }

    /**
     * 上传图片
     */
    public function uploadfile()
    {
        header('Content-Type:text/plain;charset=utf-8 ');
        //移动到框架应用根目录/public/uploads/ 目录下
        try {
            $file = request()->file('thumb');
            if ($file) {
                //1MB=1024(KB)=1048576(B)
                //上传图片大小1MB
                //1MB=1048576(B)
                //2MB=2097152(B)
                //3MB=3145728(B)
                //4MB=4194304(B)
                //5MB=5242880(B)
                $info = $file->validate(['size' => 1048576, 'ext' => 'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $imagesurl = '/public/uploads/' . $info->getSaveName();
                    $returndata = [];
                    $returndata['imagesurl'] = $imagesurl;
                    $imagesinfo = $info->getInfo();
                    $data = [];
                    $data['images_name'] = $imagesinfo['name'];
                    $data['images_url'] = $imagesurl;
                    $data['images_base'] = DataReturn::imagebase64(config('domain_name_url') . $imagesurl);
                    $data['images_size'] = $imagesinfo['size'];
                    $data['images_type'] = $imagesinfo['type'];
                    $data['inputtime'] = time();
                    Photosmodel::addService($data);
                    DataReturn::returnJson('success', "上传成功", $returndata);
                } else {
                    throw new \Exception($file->getError());
                }
            } else {
                throw new \Exception("请选择要上传的图片");
            }
        } catch (\Exception $e) {
            DataReturn::returnJson('error', $e->getMessage());
        }
    }
}
